package com.community.zhihuishequ.view.collect;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.community.zhihuishequ.R;
import com.community.zhihuishequ.db.HouseData;
import com.community.zhihuishequ.view.base.BaseActivity;
import com.raizlabs.android.dbflow.sql.language.CursorResult;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CollectDraftListActivity extends BaseActivity {

    @BindView(R.id.toolBar)
    Toolbar toolBar;
    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;
    @BindView(R.id.rv_draft)
    RecyclerView rvDraft;

    private List<HouseData> houseDataList;
    private ContentAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_draft_list);
        unbinder = ButterKnife.bind(this);
        initActionBar(toolBar, true);
        toolBar.setNavigationIcon(R.drawable.ic_back);
        initData();
        initList();
    }

    private void initData() {
        houseDataList = new ArrayList<>();
        houseDataList = SQLite.select()
                .from(HouseData.class)
                .queryList();
    }

    private void initList() {
        adapter = new ContentAdapter(R.layout.item_collect_darft, houseDataList);
        rvDraft.setLayoutManager(new LinearLayoutManager(this));
        rvDraft.setAdapter(adapter);
    }

    private class ContentAdapter extends BaseQuickAdapter<HouseData, BaseViewHolder> {

        public ContentAdapter(int layoutResId, @Nullable List<HouseData> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, HouseData item) {
            helper.setText(R.id.tv_name, item.owner);
            helper.setText(R.id.tv_create_time, "生成时间：" + item.createTime);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
